package services;

import dto.BannerDto;
import models.App;
import models.Banner;
import play.i18n.Lang;

import java.util.List;

/**
 * Created by Gipxy group
 * Date: 3/17/12
 */
public class BannerService {

    public static BannerDto createDto(Banner app) {
        BannerDto dto = new BannerDto();
        dto.id = app.id;
        dto.code = app.code;
        dto.title = app.title;
        dto.url = app.url;
        dto.language = app.language;
        dto.url = app.url;
        dto.order = app.order;
        dto.type = app.type;
        dto.photoAsBytes = app.photo;

        return dto;
    }

    //check exits: language + code
    public static boolean isExisted(String code, String language) {
        List<App> fetch = Banner.all(App.class).filter("code", code).filter("language", language).fetch();
        return fetch.size()>0;
    }

    public static Banner merge(BannerDto dto) {
        Banner merged=null;
        if (dto.id!=null) {
            merged = Banner.getByKey(Banner.class,dto.id);
        }
        if (merged==null){
            merged = new Banner();
        }

        merged.title = dto.title;
        merged.code = dto.code;
        merged.url=dto.url;
        System.out.println("LANG="+dto.language);
        merged.language = Lang.get();

        merged.type = dto.type;
        merged.order = dto.order;

        if (dto.photo!=null)
            merged.photo = dto.photo.asBytes();

        merged.save();

        return merged;
    }

}
